<template>
  <div class="xtx-cart-page">
    <div class="container m-top-20">
      <div class="cart">
        <table>
          <thead>
            <tr>
              <!--
                1. 点击全选  子项跟着一起变
                2. 当所有子项都选中的时候 全选才会选中
               -->
              <th width="120"><XtxCheckbox @change="allChange" :model-value="$store.getters['cart/isAllCheck']">全选</XtxCheckbox></th>
              <th width="400">商品信息</th>
              <th width="220">单价</th>
              <th width="180">数量</th>
              <th width="180">小计</th>
              <th width="140">操作</th>
            </tr>
          </thead>
          <!-- 有效商品 -->
          <tbody>
            <tr v-for="i in cartList" :key="i.id">
              <!--
                使用vuex中的数据通过父传子的方式 实现单选框选中状态的初始化 数据驱动视图
                当切换单选框内部的选中状态的时候 把这个状态的最新值反向交给vuex中对应的数据
               -->
              <td><XtxCheckbox :model-value="i.selected" @change="(selected)=>checkChange(i,selected)"/></td>
              <td>
                <div class="goods">
                  <RouterLink to="/"><img :src="i.picture" alt=""></RouterLink>
                  <div>
                    <p class="name ellipsis">{{i.name}}</p>
                    <!-- 选择规格组件 -->
                  </div>
                </div>
              </td>
              <td class="tc">
                <p>&yen;{{i.price}}</p>
                <p>比加入时降价 <span class="red">&yen;20.00</span></p>
              </td>
              <td class="tc">
                <!--
                  组件传参的时候只有一种情况可以不加冒号 识别为字符串
                  <el-button type="success">

                  识别data中的数据 + 识别具体的js类型 都需要加冒号
                  <Son :title="title"/>
                  <Son :list="[1,2,3]"/>
                 -->
                <XtxNumber :is-label="false" :model-value="i.count" />
              </td>
              <td class="tc"><p class="f16 red">&yen;{{i.price * i.count}}</p></td>
              <td class="tc">
                <p><a class="green" href="javascript:;">删除</a></p>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <!-- 操作栏 -->
      <div class="action">
        <div class="batch">
          共 {{cartCount}} 件商品，已选择 {{seletedCount}} 件，商品合计：
          <span class="red">¥{{selectedPrice}}</span>
        </div>
        <div class="total">
          <XtxButton type="primary" @click="goOrder">下单结算</XtxButton>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { useStore } from 'vuex'
import { computed } from 'vue'
import Messsage from '@/components/Message/index'
import { useRouter } from 'vue-router'
export default {
  name: 'XtxCartPage',
  setup () {
    const store = useStore()
    const cartList = computed(() => {
      return store.state.cart.cartList
    })

    const checkChange = (i, selected) => {
      console.log(i, selected)
      // 修改到vuex对应的位置上去 mutation
      // 需要额外的一个参数用来查找当前到底要修改谁？ skuId
      // store.commit('cart/singleCheck', {
      //   skuId: i.skuId,
      //   selected
      // })
      store.dispatch('cart/singleCheck', {
        skuId: i.skuId,
        count: i.count,
        selected
      })
    }

    // 映射计算值
    const seletedCount = computed(() => store.getters['cart/selectedCount'])
    const cartCount = computed(() => store.getters['cart/cartCount'])
    const selectedPrice = computed(() => store.getters['cart/selectedPrice'])

    // 全选
    const allChange = (selected) => {
      console.log(selected)
      // vuex cartList中的每一项的selected都改成和我一样
      // 调用全选接口 -> 后端把所有项都改成和你一样 -> 重新拉取所有列表 -> cartList
      store.dispatch('cart/allCheck', selected)
    }
    const router = useRouter()
    const goOrder = () => {
      if (seletedCount.value === 0) {
        return Messsage({ type: 'warn', text: '请至少选择一个商品' })
      }
      router.push('/settled')
    }
    return {
      cartList,
      checkChange,
      seletedCount,
      cartCount,
      selectedPrice,
      allChange,
      goOrder
    }
  }
}
</script>
<style scoped lang="less">
.tc {
  text-align: center;
  .xtx-numbox {
    margin: 0 auto;
    width: 120px;
  }
}
.red {
  color: @priceColor;
}
.green {
  color: @xtxColor
}
.f16 {
  font-size: 16px;
}
.goods {
  display: flex;
  align-items: center;
  img {
    width: 100px;
    height: 100px;
  }
  > div {
    width: 280px;
    font-size: 16px;
    padding-left: 10px;
    .attr {
      font-size: 14px;
      color: #999;
    }
  }
}
.action {
  display: flex;
  background: #fff;
  margin-top: 20px;
  height: 80px;
  align-items: center;
  font-size: 16px;
  justify-content: space-between;
  padding: 0 30px;
  .xtx-checkbox {
    color: #999;
  }
  .batch {
    a {
      margin-left: 20px;
    }
  }
  .red {
    font-size: 18px;
    margin-right: 20px;
    font-weight: bold;
  }
}
.tit {
  color: #666;
  font-size: 16px;
  font-weight: normal;
  line-height: 50px;
}
.xtx-cart-page {
  .cart {
    background: #fff;
    color: #666;
    table {
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 24px;
      th,td{
        padding: 10px;
        border-bottom: 1px solid #f5f5f5;
        &:first-child {
          text-align: left;
          padding-left: 30px;
          color: #999;
        }
      }
      th {
        font-size: 16px;
        font-weight: normal;
        line-height: 50px;
      }
    }
  }
}
</style>
